package com.bishe.psy.interceptor;

import com.bishe.psy.config.AppConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with Intellij IDEA.
 * Description: 登录拦截器，返回true，可以继续往下执行，返回false，不能继续往下执行
 * User: L
 * Date: 2024-04-10
 * Time: 9:46
 */

@Component
public class LoginInterceptor implements HandlerInterceptor {

    // preHandle 前置处理，继承HandlerInterceptor，重写该方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取当前用户的session，判断当前用户是否登录
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(AppConfig.USER_SESSION) != null) {
            //表示当前用户已经处于登录状态
            return true;
        }

        //当前用户没有登录，跳转回登录界面
        response.sendRedirect("/sign-in.html");
        return false;
    }
}
